package com.tenement.controller;

import com.mybatisflex.core.paginate.Page;
import com.tenement.common.Axios;
import com.tenement.enums.RespBusiness;
import com.tenement.vo.QueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.Autowired;
import com.tenement.entity.Dept;
import com.tenement.service.DeptService;
import org.springframework.web.bind.annotation.RestController;
import java.io.Serializable;
import java.util.List;

/**
 *  控制层。
 *
 * @author BertXie
 * @since 1.0.1
 */
@RestController
@RequestMapping("/dept")
@RequiredArgsConstructor
@Api(tags = "部门")
@Validated
public class DeptController {


    private final DeptService deptService;


    @ApiOperation("操作数据")
    @PutMapping("operator")
    public Axios operator(@RequestBody @Validated Dept dept) {
        return deptService.operator(dept) > 0 ? Axios.success("操作成功") : Axios.error(RespBusiness.FAIL);
    }


    @ApiOperation("删除部门")
    @DeleteMapping("delete/{id}")
    public Axios delete(@PathVariable Long id) {
        return deptService.delete(id) > 0 ? Axios.success("删除成功") : Axios.error(RespBusiness.FAIL);
    }


    @ApiOperation("获取树形结构的部门")
    @GetMapping("treeNode")
    public Axios treeNode() {
        return Axios.success(deptService.treeNode());
    }

    @ApiOperation("按照条件获取树形结构的部门")
    @PreAuthorize("@exp.hasAuthority('tenement:dept:list')")
    @PostMapping("list")
    public Axios list(@RequestBody QueryVo queryVo) {
        return Axios.success(deptService.list(queryVo));
    }

    /**
     * 根据主键获取详细信息。
     *
     * @param id 主键
     * @return 详情
     */
    @ApiOperation("根据id获取部门")
    @GetMapping("oneId/{id}")
    public Axios oneId(@PathVariable Serializable id) {
        return Axios.success(deptService.getById(id));
    }


}
